package com.shundr.auth.common.view;

import com.shundr.auth.R;
import com.shundr.auth.common.model.VersionInfo;
import com.shundr.auth.frame.constant.LContext;
import com.shundr.auth.frame.service.UpgradeService;
import com.shundr.auth.frame.util.ToastHelper;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.WindowManager;
import android.webkit.WebView;
import android.widget.Button;

/**
 * 下载更新提示框 DialodUpdateApp
 * 
 * @author larry 2015-5-8 下午8:56:32
 * @version 1.0.0
 *
 */
public class DialodUpdateApp extends Dialog {
	private Context context;
	private Button btnUpdateApp;
	private Button btnNotUpdate;
	private VersionInfo mInfo;

	public DialodUpdateApp(Context context, VersionInfo info) {
		super(context, R.style.dialog_style);
		this.context = context;
		this.mInfo = info;
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.view_dialog_update_app);

		WindowManager.LayoutParams params = this.getWindow().getAttributes();
		params.width = (int) (LContext.width * 0.8);
		params.height = (int) (LContext.height * 0.6);
		this.getWindow().setAttributes(params);
		setCanceledOnTouchOutside(false);
		setCancelable(false);
		btnUpdateApp = (Button) findViewById(R.id.btn_update_app);
		btnNotUpdate = (Button) findViewById(R.id.btn_cancel);
		btnUpdateApp.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				ToastHelper.toastLong(getContext(), "请等待完成更新后，重新登录！");
				Intent intent = new Intent(context, UpgradeService.class);
				intent.putExtra("url", mInfo.getAppDownloadUrl());
				intent.putExtra("flag", true);
				context.startService(intent);
				if (mInfo.getIsForceUpdate() != 1) {
					dismiss();
				} else {
					btnUpdateApp.setEnabled(false);
				}
				
			}
		});

		if (mInfo.getIsForceUpdate() == 1) {
			// btnNotUpdate.setText("退出应用");
			btnNotUpdate.setVisibility(View.GONE);
		}

		btnNotUpdate.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// NotificationUtils.updateNotifys(context, "舜达天下司机版有新版本啦",
				// "点击立即下载更新");
				if (mInfo.getIsForceUpdate() == 1) {

				}
				dismiss();
			}
		});

		WebViewWithProgress webViewProgress = (WebViewWithProgress) findViewById(R.id.webview);
		WebView mWebView = webViewProgress.getWebView();
		mWebView.getSettings().setJavaScriptEnabled(true);
		mWebView.getSettings().setSupportZoom(true);
		// //设置webview为单列显示，是一些大图片适应屏幕宽度
		// mWebView.getSettings().setLayoutAlgorithm(LayoutAlgorithm.SINGLE_COLUMN);
		mWebView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);

		// mWebView.loadUrl("http://m.shundr.com");
		mWebView.loadUrl(mInfo.getUpdateShowUrl());

	}

}
